package com.ljkj.cordial.chat.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.lanjiu.lib.imclient.bus.EventConstants;
import com.lanjiu.lib.imclient.bus.MessageEvent;
import com.lanjiu.lib.imclient.dto.GroupMemberDto;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.SearchBean;
import com.ljkj.cordial.chat.dao.AlbumTableBean;
import com.ljkj.cordial.chat.ui.AppEventActivity;
import com.ljkj.cordial.chat.ui.views.AddFriendView;
import com.ljkj.cordial.chat.util.AlbumManageUtil;
import com.nic.view.util.JToast;
import com.nic.view.widget.CircleImageView;
import com.nic.view.widget.ContainsEmojiEditText;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 加好友/加群
 */
public class AddFriendActivity extends AppEventActivity<AddFriendView> {


    @BindView(R.id.iv_user)
    CircleImageView ivUser;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_id)
    TextView tvId;
    @BindView(R.id.et_leave_msg)
    ContainsEmojiEditText etLeaveMsg;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    private int type;
    private SearchBean item;
    private String mFriendUserType;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_add_friend;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        item = (SearchBean) getIntent().getSerializableExtra(Canstants.KEY_SEARCH_RESULT_INFO);
        type = getIntent().getIntExtra(Canstants.KEY_SEARCH_RESULT, Canstants.KEY_SEARCH_FRIEND);
        if (Canstants.KEY_SEARCH_FRIEND == type) {
            //加好友
            getToolbar().setTitle("添加好友");
            tvHint.setText("申请好友留言");
            if (item != null) {
                tvName.setText(item.getName());
                if (!TextUtils.isEmpty(item.getUserId())) {
                    mFriendUserType = IMSContacts.UserType.REGISTERED_USER;
                    tvId.setText(item.getUserId());
                }
                if (!TextUtils.isEmpty(item.getTouristId())) {
                    mFriendUserType = IMSContacts.UserType.TOURISTS_USER;
                    tvId.setText(item.getTouristId());
                }
                if (!TextUtils.isEmpty(item.getAvatars())) {
                    AlbumManageUtil.getInstance().downLoadFileFriend(item.getAvatars(), tvId.getText().toString(), ivUser);
                }
            }
        } else if (Canstants.KEY_SEARCH_GROUP == type) {
            //加群组
            getToolbar().setTitle("添加群");
            tvHint.setText("申请加群留言");
            if (item != null) {
                tvName.setText(item.getName());
                if (!TextUtils.isEmpty(item.getGroupId())) {
                    tvId.setText(item.getGroupId());
                }
            }
        }

        getToolbarRightTv().setText("提交");
        getToolbarRightTv().setVisibility(View.VISIBLE);
        getToolbarRightTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Canstants.KEY_SEARCH_FRIEND == type) {
                    String userId = mFriendUserType.equals(IMSContacts.UserType.REGISTERED_USER) ? item.getUserId() : item.getTouristId();
                    obtainView().doApplyFriend(userId, item.getName(), item.getAvatars(), mFriendUserType, etLeaveMsg.getText().toString());
                } else {
                    obtainView().doApplyGroup(item.getGroupId(), item.getName(), item.getGroupOwnerId(), etLeaveMsg.getText().toString());
                }
            }
        });
    }

    public static void jumpTo(Activity activity, int type, SearchBean item) {
        Intent intent = new Intent(activity, AddFriendActivity.class);
        intent.putExtra(Canstants.KEY_SEARCH_RESULT, type);
        intent.putExtra(Canstants.KEY_SEARCH_RESULT_INFO, item);
        activity.startActivityForResult(intent, Canstants.REQUEST_CODE_BACK);
    }


    @Override
    protected AddFriendView bindNettyMessageView() {
        return new AddFriendView(new AddFriendView.AddFriendCallback() {

            @Override
            public void onAddFriendCallback(int code, String msg) {
                JToast.showShort(AddFriendActivity.this, msg);
                if (code == IMSContacts.ResponseCode.SUCCESS) {
                    setResult(Canstants.REQUEST_CODE_BACK);
                    finish();
                }
            }

            @Override
            public void onAddGroupCallback(int code, String msg, GroupMemberDto data) {
                JToast.showShort(AddFriendActivity.this, msg);
                if (code == IMSContacts.ResponseCode.SUCCESS) {
                    setResult(Canstants.REQUEST_CODE_BACK);
                    finish();
                }
            }
        });
    }
}
